package com.libraryinformation.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libraryinformation.dao.WeatherDao;
import com.libraryinformation.domain.Backlog;
import com.libraryinformation.domain.Borrow;
import com.libraryinformation.domain.Weather;
import com.libraryinformation.domain.WeatherVO;
import com.libraryinformation.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Transactional
@RequestMapping("/weather")
public class WeatherController {
    @Autowired
    private WeatherDao weatherDao;
    /**
     * 新增或更新监控指标
     *
     * @param weather
     * @return
     */
    @PostMapping("/insertOrUpdate")
    public Result insertOrUpdate(@RequestBody Weather weather) {
        weather.setInsertTime(TimeUtil.convertDateToString());
        int flag = weatherDao.insertOrUpdateWeather(weather);
        Integer code = flag == 1 ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = flag == 1 ? "操作成功" : "操作失败，请重试！";
        return new Result(code, flag, msg);
    }
    /**
     * 更新指标状态
     *
     * @param weather
     * @return
     */
    @PutMapping("/updateStatus")
    public Result update(@RequestBody Weather weather) {
        int flag = weatherDao.updateByStatus(weather.getStatus(), weather.getId());
        return new Result(flag == 1 ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /**
     * 删除监控指标,即更新deleted状态
     *
     * @param id
     * @return
     */
    @PutMapping("/updateDeleted/{id}")
    public Result delete(@PathVariable Long id) {
        int flag = weatherDao.updateByDeleted(id);
        return new Result(flag == 1 ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /**
     * 根据id查询某个指标详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public Result getById(@PathVariable Long id) {
        Weather weather = weatherDao.selectById(id);
        Integer code = weather != null ? Code.GET_OK : Code.GET_ERR;
        String msg = weather != null ? "查询成功" : "数据查询失败，请重试！";
        return new Result(code, weather, msg);
    }

    /**
     * 查询所有监控指标
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result getAll() {
        List<WeatherVO> weatherList = weatherDao.getAll();
        Integer code = weatherList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = weatherList != null ? "" : "数据查询失败，请重试！";
        if (weatherList != null) {
            weatherList.forEach(weatherVO -> resetPercentage(weatherVO));
        }
        return new Result(code, weatherList, msg);
    }
    /**
     * 分页查询监控指标
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/selectAllByPage")
    public Result getByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        IPage<Weather> weatherPage = new Page<>(page, size);
        IPage<WeatherVO> mapIPage = weatherDao.selectAllPage(weatherPage);
        Integer code = mapIPage != null ? Code.GET_OK : Code.GET_ERR;
        String msg = mapIPage != null ? "" : "数据查询失败，请重试！";
        if (mapIPage != null) {
            mapIPage.getRecords().forEach(weatherVO -> resetPercentage(weatherVO));
        }
        return new Result(code, mapIPage, msg);
    }

    private void resetPercentage(WeatherVO weatherVO) {
        if (StringUtils.isEmpty(weatherVO.getValue())) return;
        switch (weatherVO.getName()) {
            case "湿度":
            case "空气温度":
                weatherVO.setPercentage(new BigDecimal(weatherVO.getValue()).setScale(2));
                break;
            case "土壤温度":
                weatherVO.setPercentage((new BigDecimal(weatherVO.getValue()).divide(new BigDecimal(35),2,RoundingMode.HALF_UP).multiply(new BigDecimal(100))).setScale(2,RoundingMode.HALF_UP));
                break;
            case "二氧化碳浓度":
                weatherVO.setPercentage((new BigDecimal(weatherVO.getValue()).divide(new BigDecimal(0.1),2,RoundingMode.HALF_UP).multiply(new BigDecimal(100))).setScale(2,RoundingMode.HALF_UP));
                break;
            case "光照度":
                weatherVO.setPercentage((new BigDecimal(weatherVO.getValue()).divide(new BigDecimal(6000),2,RoundingMode.HALF_UP).multiply(new BigDecimal(100))).setScale(2,RoundingMode.HALF_UP));
                break;
            default:
                weatherVO.setPercentage(BigDecimal.ZERO);
        }
    }
}
